package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DeleteOperation implements IOperation{
    @Override
    public void work(BookList booklist) {
        System.out.println("删除图书");
        System.out.println("请输入你要删除的图书：");
        Scanner sc=new Scanner(System.in);
        String name=sc.nextLine();
        int currentSize= booklist.getUsedSize();
        int index=-1;
        int i=0;
        for(i=0;i< currentSize;i++){
            Book book=booklist.getBooks(i);
            if(book.getName().equals(name)){
                index=i;
                break;
            }
        }
        if(i>=currentSize){
            System.out.println("书架没有你要删除的书");
        }else{
            for(int j=index;j<currentSize-1;j++){
                Book book=booklist.getBooks(j+1);
                booklist.setBooks(j,book);
            }
            booklist.setUsedSize(currentSize-1);
            System.out.println("删除图书成功");
        }
    }
}
